package com.xh.bussiness.system.controller;


import com.xh.bussiness.system.bean.request.PaperTemplateRequest;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.model.PaperTemplate;
import com.xh.bussiness.system.service.IPaperTemplateService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/system/paperTemplate")
public class PaperTemplateController {

    @Autowired
    private IPaperTemplateService paperTemplateService;

    /**
     * 分页数据查询
     *
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/queryPage")
    public XhPage queryPage(XhPage page, PaperTemplateRequest bean) {
        page = paperTemplateService.queryPaperTemplatePage(page, bean);
        return page;
    }

    /**
     * 新增和保存
     * @param paperTemplate 实体对象
     * @return
     */
    @RequestMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Valid @RequestBody PaperTemplate paperTemplate){
        if (StringUtils.isEmpty(paperTemplate.getId())) {
            if (!CollectionUtils.isEmpty(paperTemplate.getSubjects())) {
                for (Dictionary subject : paperTemplate.getSubjects()) {
                    List<PaperTemplate> exists = paperTemplateService.queryBySubjectCode(subject.getCode(), paperTemplate.getType());
                    if (!CollectionUtils.isEmpty(exists)) {
                        PaperTemplate existsTemplate = exists.get(0);
                        existsTemplate.setTemplatePath(paperTemplate.getTemplatePath());
                        existsTemplate.setName(paperTemplate.getName());
                        paperTemplateService.update(existsTemplate);
                        continue;
                    }
                    PaperTemplate template = new PaperTemplate();
                    BeanUtils.copyProperties(paperTemplate, template);
                    template.setSubjectCode(subject.getCode());
                    template.setSubjectName(subject.getName());

                    paperTemplateService.insert(template);
                }
            }
        } else {
            paperTemplateService.update(paperTemplate);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        paperTemplateService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }



    @RequestMapping("/batchChangeStatus")
    @Transactional
    public AjaxResult batchChangeStatus(String[] ids, Integer status) {
        paperTemplateService.batchChangeStatus(ids, status);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    @RequestMapping("/queryBySubjectCode")
    public AjaxResult queryBySubjectCode(String subjectCode) {
        if (StringUtils.isEmpty(subjectCode)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, paperTemplateService.queryBySubjectCode(subjectCode,null));
    }



}
